/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.provider;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

import com.isotne.glidelibrary.utils.OhosGlide;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.provider.entity.NewInfo;

import java.util.List;

import static com.xuexiang.xui_lib.XUI.getContext;

/**
 * @since 2021-04-23
 */
public class NewsCardViewListProvider extends BaseItemProvider {

    private List<NewInfo> data;

    public NewsCardViewListProvider(List<NewInfo> data) {
        this.data = data;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        NewInfo item = data.get(i);
        Component layout = LayoutScatter
            .getInstance(getContext())
            .parse(ResourceTable.Layout_news_card_view_list_item, null, false);
        Text tv_user_name = (Text) layout.findComponentById(ResourceTable.Id_tv_user_name);
        tv_user_name.setText(item.getUserName());
        Text tv_tag = (Text) layout.findComponentById(ResourceTable.Id_tv_tag);
        tv_tag.setText(item.getTag());
        Text tv_title = (Text) layout.findComponentById(ResourceTable.Id_tv_title);
        tv_title.setText(item.getTitle());
        Text tv_summary = (Text) layout.findComponentById(ResourceTable.Id_tv_summary);
        tv_summary.setText(item.getSummary());
        Text tv_praise = (Text) layout.findComponentById(ResourceTable.Id_tv_praise);
        tv_praise.setText(item.getPraise() == 0 ? "点赞" : String.valueOf(item.getPraise()));
        Text tv_comment = (Text) layout.findComponentById(ResourceTable.Id_tv_comment);
        tv_comment.setText(item.getComment() == 0 ? "评论" : String.valueOf(item.getComment()));
        Text tv_read = (Text) layout.findComponentById(ResourceTable.Id_tv_read);
        tv_read.setText("阅读量 " + item.getRead());
        Image iv_image = (Image) layout.findComponentById(ResourceTable.Id_iv_image);
        iv_image.setScaleMode(Image.ScaleMode.INSIDE);
        OhosGlide.with(getContext()).load(item.getImageUrl()).into(iv_image);
        return layout;
    }
}
